//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "adminActions\fn_teleportToBase.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_params","_caller_name","_caller_uid","_player_uid","_player_name","_player_info","_action","_authorized","_player","_player_id","_counter","_player_pos","_new_pos","_all_players","_marker_name"];
PARAMS_3(_params,_caller_name,_caller_uid);

_authorized = _params select 0;
_action = _params select 1;
_player = _params select 2;
_player_uid = _params select 3;
_player_name = _params select 4;

if (isServer) then {
	_player_info = [];
	_index = GVAR(PlayerIndex) find _player_uid;
	if (_index >= 0) then {
		_player_info = GVAR(PlayerInfo) select _index;
	};
	_player_pos = [];
	if (!isnil "D_FLAG_BASE") then { _player_pos = position D_FLAG_BASE; };
	if (count _player_pos <= 0 && {markerType "base_respawn" != ""}) then { _player_pos = markerPos "base_respawn"; };
	
	_marker_name = switch (side _player) do {
		case west : { "respawn_west" };
		case east : { "respawn_east" };
		case resistance : { "respawn_guerrila" };
		default { "" };
	};
	
	if (count _player_pos <= 0 && {markerType _marker_name != ""}) then { _player_pos = markerPos _marker_name; };
	
	if (count _player_info > 0) then {
		// _player_info set [0, _name];	// Player name at login
		// _player_info set [1, _uid];		// Player UID
		// _player_info set [2, _id];		// Player DirectPlay ID at login
		// _player_info set [3, MSO_AA_PLAYER_ID_COUNT]; // Player ID at login
		// _player_info set [4, time];		// Player first login time
		// _player_info set [5, getPos player];// Initial Position
		// _player_info set [6, time];		// Player last login time
		// _player_info set [7, 0];		// Player jails
		// _player_info set [8, 0];		// Player kicks
		// _player_info set [9, 0];		// Player bans
		// _player_info set [10, 0];		// Player teleports
		// _player_info set [11, 0];		// Player vehicle crashes
		
		_player_id = (_player_info select 3);
		_player_name = (_player_info select 0);
		_player_pos = (_player_info select 5);
		
		// Increment the counter of the actions performed against the user
		_counter = (_player_info select 10) + 1;
		_player_info set [10, _counter];
		GVAR(PlayerInfo) set [_index, _player_info];
	} else {
		ERROR_1("Missing Player Info",_player_uid);
	};		
	
	_player = objNull;
	_all_players = playableUnits;
	if (!isMultiPlayer) then { _all_players = [player]; };
	{ if (getPlayerUID _x == _player_uid) exitWith { _player = _x; };
	} foreach _all_players;

	if (!isNull _player) then {
		if (count _player_pos <= 0) then { _player_pos = position _player; };
		private ["_empty_pos"];
		_empty_pos = _player_pos findEmptyPosition[0 , 20];
		if (count _empty_pos <= 0) then { _empty_pos = _player_pos; };
		_empty_pos set [2,0.1];
		OSETVARP(_player,QGVAR(PLAYER_START_POS),_empty_pos);
	} else {
		ERROR_1("Player Not Found",_player_uid);
	};
};
if (!isDedicated) then {
	if (_player_uid == getPlayerUID player) then {
		private ["_vec","_timeout"];
		_timeout = time + 10;
		waituntil { count(PGETVAR(QGVAR(PLAYER_START_POS),[])) > 0 || _timeout < time };
		_player_pos = PGETVAR(QGVAR(PLAYER_START_POS),(position player));
		if ((_player_pos distance player) > 30) then {
			titleText [localize "STR_MSO_AA_PLAYER_TELEPORTED", "BLACK",0.1];
			sleep 1;
			PSETVARP(QGVAR(PLAYER_START_POS),[]);
			while {vehicle player != player} do {
				_vec = vehicle player;
				player action ["eject", _vec];
				waituntil {vehicle player != _vec};
			};
			player setVelocity [0,0,0];
			player setPos _player_pos;
			titleFadeOut 5;
		};
	} else {

	};
};

TRACE("End");
TMFPROFILERSTOP;
